﻿using System.Collections;
using System.Threading;
using NHibernate;

namespace _5E.NHibernateRepository.SessionStorage
{
    public class ThreadSessionStorageContainer : ISessionStorageContainer
    {
        private static readonly Hashtable s_nhibernateSessions = new Hashtable();

        public ISession CurrentSession
        {
            get
            {
                ISession session = null;

                if (s_nhibernateSessions.Contains(GetThreadName()))
                    session = s_nhibernateSessions[GetThreadName()] as ISession;

                return session;
            }
        }

        public void Store(ISession session)
        {
            if (s_nhibernateSessions.Contains(GetThreadName()))
                s_nhibernateSessions[GetThreadName()] = session;
            else
                s_nhibernateSessions.Add(GetThreadName(), session);
        }

        private static string GetThreadName()
        {
            return Thread.CurrentThread.Name;
        }
    }
}
